package week02;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Week02OutputWritable implements WritableComparable<Week02Output> {

    private Week02Output week02Output;

    public Week02OutputWritable(Week02Output week02Output) {
        this.week02Output = week02Output;
    }

    public String week02OutputToString() {
        String output = week02Output.getMobile() + " "
                + week02Output.getUpSize() + " "
                + week02Output.getDownSize() + " "
                + week02Output.getTotalSize();
        return output;
    }

    public Week02Output get() {
        return week02Output;
    }

    @Override
    public int compareTo(Week02Output o) {
        return week02Output.getMobile().compareTo(o.getMobile());
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(week02OutputToString());
    }


    @Override
    public void readFields(DataInput dataInput) throws IOException {
        String line = dataInput.readUTF();
        if(line != null && line.length() > 0) {
            String[] fieldArray = line.split(" ");
            if(fieldArray != null && fieldArray.length == 3) {
                week02Output = new Week02Output();
                week02Output.setMobile(fieldArray[0]);
                week02Output.setUpSize(Integer.valueOf(fieldArray[1]));
                week02Output.setDownSize(Integer.valueOf(fieldArray[2]));
                week02Output.setTotalSize(Integer.valueOf(fieldArray[3]));
            }
        }
    }
}
